package com.cloud.basic.TcpAndUdp;


import java.io.*;
import java.net.SocketAddress;
import java.util.Arrays;

/**
 * @program: basic
 * @description: 响应信息实体的结构定义
 * @author: Cloud
 * @create: 2021/10/11 15:48:49
 */
public class ResponseMessage {

    // 总长度
    private int totalLen;

    // 对应接收到的消息的id
    private int repid;

    // 响应数据
    private byte[] data;

    // 发送次数
    private int sendCount = 0;

    // 接收状态 0 正确接收 其他：错误
    private int state = 0;

    // 应答方的时间
    private Long restTime;

    // 最后一次发送时间
    private Long lastSendTime = System.currentTimeMillis();

    // 接收者的地址
    private SocketAddress remoteAddr;

    public ResponseMessage(int repid, byte[] data, int state){
        this.repid = repid;
        this.data = data;
        this.state = state;
        // 4+4+4 是因为每一个int类型的长度为4个字节
        this.totalLen = 4 + 4 + 4 + data.length;
    }

    /**
     * 功能描述: 构造器接收到udp数据封装为一个tcp的ResponseMessage对象
     *
     * @param udpData
     * @return null
     * @author cloud
     * @date  2021/10/11 16:04
     */
    public ResponseMessage(byte[] udpData) {
        ByteArrayInputStream inputStream = new ByteArrayInputStream(udpData);
        DataInputStream dataInputStream = new DataInputStream(inputStream);
        try {
            this.totalLen = dataInputStream.readInt();
            this.repid = dataInputStream.readInt();
            this.state = dataInputStream.readInt();
            this.data = new byte[totalLen - 4 - 4 - 4];
            dataInputStream.readFully(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public byte[] toByte(){
        ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
        DataOutputStream dataOutputStream = new DataOutputStream(arrayOutputStream);
        try {
            // 读和写要一一对应
            dataOutputStream.writeInt(totalLen);
            dataOutputStream.writeInt(repid);
            dataOutputStream.write(data);
            dataOutputStream.writeInt(state);
            dataOutputStream.flush();
            return arrayOutputStream.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int getTotalLen() {
        return totalLen;
    }

    public void setTotalLen(int totalLen) {
        this.totalLen = totalLen;
    }

    public byte[] getData() {
        return data;
    }

    public void setData(byte[] data) {
        this.data = data;
    }

    public int getSendCount() {
        return sendCount;
    }

    public void setSendCount(int sendCount) {
        this.sendCount = sendCount;
    }

    public Long getLastSendTime() {
        return lastSendTime;
    }

    public void setLastSendTime(Long lastSendTime) {
        this.lastSendTime = lastSendTime;
    }

    public SocketAddress getRemoteAddr() {
        return remoteAddr;
    }

    public void setRemoteAddr(SocketAddress remoteAddr) {
        this.remoteAddr = remoteAddr;
    }

    public int getRepid() {
        return repid;
    }

    public void setRepid(int repid) {
        this.repid = repid;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public Long getRestTime() {
        return restTime;
    }

    public void setRestTime(Long restTime) {
        this.restTime = restTime;
    }

    @Override
    public String toString() {
        return "ResponseMessage{" +
                "totalLen=" + totalLen +
                ", repid=" + repid +
                ", data=" + Arrays.toString(data) +
                ", sendCount=" + sendCount +
                ", state=" + state +
                ", restTime=" + restTime +
                ", lastSendTime=" + lastSendTime +
                ", remoteAddr=" + remoteAddr +
                '}';
    }
}
